package com.myteam.mystory.fragment;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.myteam.mystory.R;
import com.myteam.mystory.adapter.AllStoryAdapter;
//import com.myteam.mystory.adapter.AllStoryAdapter;
import com.myteam.mystory.quickaction.ActionItem;
import com.myteam.mystory.quickaction.QuickAction;
import com.myteam.mystory.service.StoriesService;

@SuppressLint("ValidFragment")
public class AllStoryFragment extends Fragment {
	private ListView liststory;
	private String[] arrNameStory;
	
	// QuickAction
	private int mSelectedRow = 0;
	private ImageView mMoreIv = null;
	private static final int ID_ADD = 1;
	private static final int ID_ACCEPT = 2;
	private static final int ID_UPLOAD = 3;
	//=====================

	public AllStoryFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View rootView = inflater.inflate(R.layout.allstory, container, false);
		liststory = (ListView) rootView.findViewById(R.id.allstory_lv_liststory);
		AllStoryAdapter adapter = new AllStoryAdapter(getActivity());
		final String[] data = { "Test 01", "Test 02", "Test 03", "Test 04", "Test 05", "Test 06", "Test 07", "Test 08", "Test 09", "Test 10", "Test 02", "Test 03", "Test 04", "Test 05", "Test 06", "Test 07", "Test 08", "Test 09", "Test 10", "Test 02", "Test 03", "Test 04", "Test 05", "Test 06", "Test 07", "Test 08", "Test 09", "Test 10" };
		adapter.setData(data);
		liststory.setAdapter(adapter);
		
		ActionItem likeCount = new ActionItem("100 like", "  Adjhkfajjgasgfeasur iyauryaif dhasufryar ajfgru sayfeaaiusfd asjgufiarga", 5);        
        final QuickAction mQuickAction 	= new QuickAction(getActivity());
        mQuickAction.addActionItem(likeCount);
		
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			@Override
			public void onItemClick(QuickAction quickAction, int pos, int actionId) {
				ActionItem actionItem = quickAction.getActionItem(pos);
				
				if (actionId == ID_ADD) { //Add item selected
					Toast.makeText(getActivity(), "Add item selected on row " + mSelectedRow, Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getActivity(), actionItem.getTitle() + " item selected on row " 
							+ mSelectedRow, Toast.LENGTH_SHORT).show();
				}	
			}
		});
		
		mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() {			
			@Override
			public void onDismiss() {
				mMoreIv.setImageResource(R.drawable.bookmark_none);
			}
		});
		
		liststory.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mSelectedRow = position; //set the selected row
				mQuickAction.show(view);
				mMoreIv = (ImageView) view.findViewById(R.id.allstory_bookmark);
			}
		});
		
		return rootView;
	}
}
